

#####################
### Load Packages ###
#####################

library(qdapRegex)

# Set this Working Directory to the Folder with the Replication Files
#setwd("C:/Steven/GDrive/Research Files/Clark-Rogers/Descriptive Representation/APSR Final Submission/Replication")

CreatePlotBin <- function(CurrentFile, Sexism)
  {
  colnames(CurrentFile) <- c("X", "Y")
    Estimates <- subset(CurrentFile, CurrentFile$X!="=")
    
    CIs <- subset(CurrentFile, CurrentFile$X=="=")
      LowerBounds <- unlist(rm_between(CIs$Y, '=[', ',', extract=TRUE))
      UpperBounds <- unlist(rm_between(CIs$Y, ',', ']', extract=TRUE))
    
    CurrentPlotBin <- data.frame(matrix(NA, 5,0))
      CurrentPlotBin$Sexism <- Sexism/100
      CurrentPlotBin$Outcome <- seq(-2,2,1)
      CurrentPlotBin$Estimate <- head(Estimates$Y,5)
        CurrentPlotBin$Estimate <- as.numeric(gsub("=", "", CurrentPlotBin$Estimate))
      CurrentPlotBin$LowerBound <- unlist(rm_between(CIs$Y, '=[', ',', extract=TRUE))
      CurrentPlotBin$UpperBound <- unlist(rm_between(CIs$Y, ',', ']', extract=TRUE))
   
    return(CurrentPlotBin)
  }
  
  SexismLevel <- seq(0,100,10)
  
  for(i in 1:length(SexismLevel))
  {
    CurrentFileName <- paste("Scripts/Figures/Estimates/Racial Resentment/StateLeg-Trust-Sexism-",SexismLevel[i],".csv", sep="")
    CurrentFile <- read.csv(CurrentFileName)
    CurrentPlotBin <- CreatePlotBin(CurrentFile, SexismLevel[i])  
    if(i==1){RunningPlotBin <- CurrentPlotBin}
    if(i>1){RunningPlotBin <- rbind(RunningPlotBin, CurrentPlotBin)}
  }

####
# Create Plots
###
  
CreatePlot <- function(CurrentData, Main)
  {
  
  customgrey <- rgb(150, 150, 150, alpha=80, maxColorValue=255, names="customgrey")
  
  plot(CurrentData$Sexism, CurrentData$Estimate,
       type="n",
       xlab="",
       ylab="",
       ylim=c(-.1,.1),
       xlim=c(0,1),
       family="B",
       xaxt="n",
       yaxt="n",
       yaxs = "i", # Removes Inner Margin
       xaxs = "i", # Removes Inner Margin
  )
  
  
  polygon(c(CurrentData$Sexism, rev(CurrentData$Sexism)), c(CurrentData$UpperBound, rev(CurrentData$LowerBound)), col=customgrey, border="NA")
  lines(CurrentData$Sexism, CurrentData$Estimate, lwd=2)
  abline(h=0)
  
  # Title
  title(main=Main, line=.5, cex.main=.75)
  
  # Axes
  axis(2, at=0, labels=c("Change in Predicted Probability"), line=1, cex.axis=.75, tick=FALSE, family="B")
  axis(2, at=c(-.1,-.05,0,.05,.1), labels=c("-0.10", "-0.05", "0.00", "0.05", "0.10"), cex.axis=.5, family="B", las=1)
 
  axis(1, at=c(0, .2,.4,.6,  .8,  1.0),
       labels=c("", "","","", "",""), cex.axis=.5, las=1,)
  
  axis(1, at=c(0, .2,.4,.6,  .8,  1.0),
       labels=c("0", "0.2","0.4","0.6", "0.8","1.0"), cex.axis=.5, las=1, line=-.75, tick=FALSE)
  
  axis(1, at=.5, labels=c("Sexism Value"), line=.25, cex.axis=.75, tick=FALSE)
  }

  
  LowestResponsive <- subset(RunningPlotBin, RunningPlotBin$Outcome==-2)
  LowResponsive <- subset(RunningPlotBin, RunningPlotBin$Outcome==-1)
  NotSureResponsive <- subset(RunningPlotBin, RunningPlotBin$Outcome==0)
  SomeResponsive <- subset(RunningPlotBin, RunningPlotBin$Outcome==1)
  VeryResponsive <- subset(RunningPlotBin, RunningPlotBin$Outcome==2)
  
  windowsFonts(
    A=windowsFont("Arial Black"),
    B=windowsFont("Cambria"),
    C=windowsFont("Comic Sans MS"),
    D=windowsFont("Symbol")
  )

tiff(filename = "Figures/Appendix/Figure-A-8.tiff", width = 7, height = 1.5, units = "in", pointsize = 12, res=1000)
      par(mfrow=c(1,5),
      mai=c(.3,.4,.25,.1),
      family="B")
      
      CreatePlot(LowestResponsive, "Never")
      CreatePlot(LowResponsive, "Some of the time")
      CreatePlot(NotSureResponsive, "About half of the time")
      CreatePlot(SomeResponsive, "Most of the time")
      CreatePlot(VeryResponsive, "Always")
    dev.off()
    
    


